package com.itcast.controller;

import com.itcast.annotation.Log;
import com.itcast.pojo.Clazz;
import com.itcast.pojo.ClazzQueryParam;
import com.itcast.pojo.PageBean;
import com.itcast.pojo.Result;
import com.itcast.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/clazzs")
@Slf4j
public class ClazzController {
    @Autowired
    private ClazzService clazzService;

    /**
     * 查询班级信息(条件)
     * @param param
     * @return
     */
    @GetMapping
    public Result listQuery(ClazzQueryParam param){
        PageBean pageBean = clazzService.listQuery(param);
        log.info("查询班级信息, {}", param);
        return Result.success(pageBean);
    }

    /**
     * 删除班级
     * @param id
     * @return
     */
    @Log
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("删除班级, {}", id);
        clazzService.delete(id);
        return Result.success();
    }

    /**
     * 新增班级
     * @return
     */
    @Log
    @PostMapping
    public Result add(@RequestBody Clazz clazz){
        log.info("新增班级, {}", clazz);
        clazzService.add(clazz);
        return Result.success();
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        Clazz clazz = clazzService.getById(id);
        log.info("根据id查询班级{}, 信息为{}",id,clazz);
        return Result.success(clazz);
    }

    /**
     * 修改班级
     * @return
     */
    @Log
    @PutMapping
    public Result update(@RequestBody Clazz clazz){
        log.info("修改班级, {}", clazz);
        clazzService.update(clazz);
        return Result.success();
    }

    /**
     * 查询所有班级
     * @return
     */
    @GetMapping("/list")
    public Result list(){
        List<Clazz> clazzList = clazzService.list();
        log.info("查询所有班级, {}", clazzList);
        return Result.success(clazzList);
    }
}
